package com.sun.lib.shadow;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/*
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */

/**
 * Created by ESSC07 on 2021/5/25.
 */
public class ShadowDrawable extends Drawable {
    private int shadowColor;
    private float shadowRadius;
    private int shadowDx;
    private int shadowDy;
    private float[] roundRadii;
    /*private int shadowLeftHeight;
    private int shadowRightHeight;
    private int shadowTopHeight;
    private int shadowBottomHeight;*/
    private boolean showLeft;
    private boolean showTop;
    private boolean showRight;
    private boolean showBottom;

    private Path path;
    private Paint paint;
    private RectF rectF;

    public ShadowDrawable() {
        path = new Path();
        rectF = new RectF();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    }

    public void updateParameter(int shadowColor, float shadowRadius, int shadowDx, int shadowDy, float[] roundRadii, boolean leftShow, boolean rightShow, boolean topShow, boolean bottomShow) {
        this.shadowColor = shadowColor;
        this.shadowRadius = shadowRadius;
        this.shadowDx = shadowDx;
        this.shadowDy = shadowDy;
        this.roundRadii = roundRadii;
        /*this.shadowLeftHeight = shadowLeftHeight;
        this.shadowRightHeight = shadowRightHeight;
        this.shadowTopHeight = shadowTopHeight;
        this.shadowBottomHeight = shadowBottomHeight;*/
        this.showLeft = leftShow;
        this.showRight = rightShow;
        this.showBottom = bottomShow;
        this.showTop = topShow;

        paint.setColor(shadowColor);
        paint.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        if (bounds.right - bounds.left > 0 && bounds.bottom - bounds.top > 0) {
            updateBoundss(bounds);
        }
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
    }

    private void updateBoundss(Rect bounds) {
        if (bounds == null) return;
        float xPadding = getShadowOffset() + Math.abs(shadowDx);
        float yPadding = getShadowOffset() + Math.abs(shadowDy);
        float left = showLeft ? bounds.left + xPadding : bounds.left;
        float right = showRight ? bounds.right - xPadding : bounds.right;
        float top = showTop ? bounds.top + yPadding : bounds.top;
        float bottom = showBottom ? bounds.bottom - yPadding : bounds.bottom;



       /* float left = bounds.left + shadowLeftHeight;
        float right = bounds.right-shadowRightHeight;
        float top = bounds.top + shadowTopHeight;
        float bottom = bounds.bottom - shadowBottomHeight;*/

        rectF.set(left, top, right, bottom);
        path.reset();
        path.addRoundRect(rectF, roundRadii, Path.Direction.CW);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    public float getShadowOffset() {
        return shadowRadius;
    }

    public RectF getRectF() {
        return rectF;
    }
}
